/**
 * 
 */
package edu.hawaii.halealohacli.command;

import static org.junit.Assert.assertEquals;
import java.util.Calendar;
import java.util.Locale;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * A test class for the EnergySince command module.
 * 
 * @author Sergey Negrashov
 * 
 */
public class TestEnergySince {

  /**
   * Url for the wattdepot server.
   */
  private String url = "http://server.wattdepot.org:8190/wattdepot/";

  /**
   * Argument array for the command.
   */
  private String args[] = { "Mokihana", "2011-11-23" };

  /**
   * Test to make sure that the method runs with valid inputs.
   */
  @Test
  public void testEnergySinceValidData() {
    EnergySince command = new EnergySince();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    boolean ok = true;
    try {
      assertEquals("EnergySince test with valid data failed",
          command.printEnergySince().length() > 0, true);
    }
    catch (NotAuthorizedException e) {
      ok = false;
    }
    catch (ResourceNotFoundException e) {
      ok = false;
    }
    catch (BadXmlException e) {
      ok = false;
    }
    catch (MiscClientException e) {
      ok = false;
    }
    command.execute();
    assertEquals("EnergySince test with valid data failed", true, ok);
  }

  /**
   * Test to make sure that the method through an exception if the source name is invalid.
   */
  @Test
  public void testEnergySinceBadSource() {
    args[0] = "BadSource";
    EnergySince command = new EnergySince();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    boolean ok = false;
    try {
      command.printEnergySince();
    }
    catch (NotAuthorizedException e) {
      ok = false;
    }
    catch (ResourceNotFoundException e) {
      ok = true;
    }
    catch (BadXmlException e) {
      ok = false;
    }
    catch (MiscClientException e) {
      ok = false;
    }
    assertEquals("EnergySince test with an invalid source", true, ok);
  }

  /**
   * Test to make sure that method does not return anything if the date argument is invalid.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testEnergySinceBadTime() throws NotAuthorizedException, ResourceNotFoundException,
      BadXmlException, MiscClientException {
    args[0] = "Ilima";
    args[1] = "Bad-Date";
    EnergySince command = new EnergySince();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("EnergySince test with a bad date", command.printEnergySince(), "");
  }

  /**
   * Test to make sure that method does not return anything if the date argument is in the future.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testEnergySinceFutureTime() throws NotAuthorizedException, ResourceNotFoundException,
      BadXmlException, MiscClientException {
    args[1] = "2034-11-13";
    EnergySince command = new EnergySince();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("EnergySince test with a bad date", command.printEnergySince(), "");
  }

  /**
   * Test to make sure that class works as expected with the current date as an argument. Since
   * wattdepot has some problems with clock skew we decrement 20 minutes from the current time.
   * 
   * @throws NotAuthorizedException should not throw and exception if the date is invalid.
   * @throws ResourceNotFoundException should not throw and exception if the date is invalid.
   * @throws BadXmlException should not throw and exception if the date is invalid.
   * @throws MiscClientException should not throw and exception if the date is invalid.
   */
  @Test
  public void testEnergySinceCurrent() throws NotAuthorizedException, ResourceNotFoundException,
      BadXmlException, MiscClientException {
    Calendar cal = Calendar.getInstance(Locale.US);
    XMLGregorianCalendar today = Tstamp.makeTimestamp(cal.getTimeInMillis());
    today = Tstamp.incrementMinutes(today, -30);
    args[1] = today.toString();
    EnergySince command = new EnergySince();
    WattDepotClient client = new WattDepotClient(url);
    command.initialize(args, client);
    assertEquals("EnergySince test with a current date", command.printEnergySince().length() == 0,
        false);
  }
}
